home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / comm / misc / elcheapofax.lha / unpacker.c < prev   
C/C++ Source or Header  |  1993-04-02  |  2KB  |  70 lines

  1.  
  2. #include "iffp/ilbm.h"
  3. #include "iffp/packer.h"
  4.  
  5. /*----------------------------------------------------------------------*
  6.  * unpacker.c Convert data from "cmpByteRun1" run compression. 11/15/85
  7.  *
  8.  * Based on code by Jerry Morrison and Steve Shaw, Electronic Arts.
  9.  * This software is in the public domain.
  10.  *
  11.  *    control bytes:
  12.  *     [0..127]   : followed by n+1 bytes of data.
  13.  *     [-1..-127] : followed by byte to be repeated (-n)+1 times.
  14.  *     -128        : NOOP.
  15.  *
  16.  * This version for the Commodore-Amiga computer.
  17.  *----------------------------------------------------------------------*/
  18.  
  19. /*----------- UnPackRow ------------------------------------------------*/
  20.  
  21. #define UGetByte()      (*source++)
  22. #define UPutByte(c)     (*dest++ = (c))
  23.  
  24. /* Given POINTERS to POINTER variables, unpacks one row, updating the source
  25.  * and destination pointers until it produces dstBytes bytes.
  26.  */
  27.  
  28. BOOL UnPackRow(BYTE **pSource, BYTE **pDest, WORD srcBytes0, WORD dstBytes0)
  29.     {
  30.     register BYTE *source = *pSource;
  31.     register BYTE *dest   = *pDest;
  32.     register WORD n;
  33.     register WORD srcBytes = srcBytes0;
  34.     register WORD dstBytes = dstBytes0;
  35.     BOOL error = TRUE;    /* assume error until we make it through the loop */
  36.     WORD minus128 = -128;  /* get the compiler to generate a CMP.W */
  37.     BYTE c;
  38.  
  39.     D(bug("unpackrow: srcBytes0=%ld dstBytes0=%ld\n",srcBytes0, dstBytes0));
  40.  
  41.     while( dstBytes > 0 )  {
  42.     if ( (srcBytes -= 1) < 0 )  goto ErrorExit;
  43.     n = UGetByte();
  44.  
  45.     if (n >= 0) {
  46.         n += 1;
  47.         if ( (srcBytes -= n) < 0 )  goto ErrorExit;
  48.         if ( (dstBytes -= n) < 0 )  goto ErrorExit;
  49.         do {  UPutByte(UGetByte());  } while (--n > 0);
  50.         }
  51.  
  52.     else if (n != minus128) {
  53.         n = -n + 1;
  54.         if ( (srcBytes -= 1) < 0 )  goto ErrorExit;
  55.         if ( (dstBytes -= n) < 0 )  goto ErrorExit;
  56.         c = UGetByte();
  57.         do {  UPutByte(c);  } while (--n > 0);
  58.         }
  59.     }
  60.     error = FALSE;    /* success! */
  61.  
  62.   ErrorExit:
  63.     *pSource = source;    *pDest = dest;
  64.     D(bug("unpackrow exit: srcBytes=%ld dstBytes=%ld n=%ld\n",srcBytes, dstBytes, n));
  65.     return(error);
  66.     }
  67.  
  68.  
  69. /* end */
  70.